﻿using System;
using System.Collections.Generic;
using System.Text;
using ProtocolVN.Framework.Core;
using System.Data.Common;
using System.Data;

namespace ProtocolVN.Framework.Win
{
    public interface ITonKho
    {        
        DataTable GetTonKho();
        long GetNguoiCapNhat();
        DateTime? GetNgayCapNhat();
        long GetNguoiPhatSinh();
        DateTime? GetNgayPhatSinh();        
        long GetKho1();        
        long GetKho2();        
    }

    public class KhoTonKho : IKhoDuLieu
    {
        public static string AttrName = "KhoTonKho";
        private List<BizRule> updateRuleList = new List<BizRule>();
        private List<BizRule> deleteRuleList = new List<BizRule>();

        #region IKhoDuLieu Members
        public bool Update(DatabaseFB db, DbTransaction dbTrans, DOPhieuX DOX)
        {
            //Dữ liệu cần cập nhật vào KHO TON KHO từ KhoTonKho
            Object obj = DOX.GetAttr(KhoPhieuLienQuan.AttrName);            
            PLTransaction plTrans = new PLTransaction(db, dbTrans);
            
            if (obj != null)
            {
                DOPhieuLienQuan PLQ = (DOPhieuLienQuan)obj;

                DOObjRel1N Obj1N = PLQ.Obj1N;                
                if (Obj1N != null)
                {      
                    //Danh sách tạo từ chương trình
                    List<DOFWObjRelData> listObjRel = KhoTonKhoHelp.GetData(
                        plTrans, Obj1N.OBJ_SRC_TYPE, Obj1N.OBJ_SRC_ID, DOX);
                    return UpdateX(plTrans, listObjRel, DOX);
                }                
            }
            else
            {
                //Danh sách tạo từ chương trình
                List<DOFWObjRelData> listObjRel = KhoTonKhoHelp.GetData(
                    plTrans, 0, 0, DOX);
                return UpdateX(plTrans, listObjRel, DOX);
            }
            return true;
        }

        private bool UpdateX(PLTransaction plTrans, List<DOFWObjRelData> listObjRel, DOPhieuX DOX)
        {
            bool flag = false;
            ITonKho data = (ITonKho)DOX; 
            long ky_id = KhoTonKhoHelp.GetKy(plTrans, data.GetNgayPhatSinh());
            //Danh sách tạo từ DB (trường hợp update)
            List<DOFWObjRelData> listObjRelDB = KhoTonKhoHelp.GetDataDesDB(plTrans, DOX,ky_id);
            if (listObjRel.Count > 0)
            {
                foreach (DOFWObjRelData ObjRel in listObjRel)
                {
                    if (KhoTonKhoHelp.Check_Exist_ID(plTrans,ky_id,
                        ObjRel.HH_ID, ObjRel.TINH_TRANG_HH, ObjRel.DES_ID, ObjRel.DES_TYPE))
                    {
                        flag = DAFWObjRelData.I.Update(plTrans, ObjRel, true,ky_id);
                        foreach (DOFWObjRelData _ObjRel in listObjRelDB)
                        {
                            if ((ObjRel.DES_ID == _ObjRel.DES_ID) && (ObjRel.DES_TYPE == _ObjRel.DES_TYPE)
                                && (ObjRel.HH_ID == _ObjRel.HH_ID) && (ObjRel.TINH_TRANG_HH == _ObjRel.TINH_TRANG_HH))
                            {
                                ObjRel.SO_LG -= _ObjRel.SO_LG;
                                ObjRel.TONG_TR_LG -= _ObjRel.TONG_TR_LG;
                            }
                        }
                    }
                    else
                    {
                        flag = DAFWObjRelData.I.Update(plTrans, ObjRel, false,ky_id);
                    }

                    if (flag == false)
                    {
                        plTrans.Rollback();
                        return flag;
                    }
                    else
                    {
                        //Cập nhật tồn kho từ kỳ hiện tại trở về sau
                        KhoTonKhoHelp.CapNhatNhapXuatChuyen(plTrans, ObjRel, 1,ky_id);   
                    }
                }
                if (listObjRelDB.Count > 0)
                {
                    //Kiểm tra trường hợp update loại bỏ 1 số hàng hóa khỏi phiếu
                    //Khi đó ta phải loại bỏ luôn các hàng hóa trong bảng phát sinh tồn kho
                    foreach (DOFWObjRelData ObjRel in listObjRelDB)
                    {
                        //Nếu không tìm thấy đối tượng trong danh sách
                        //-> người dùng đã loại bỏ hàng hóa đó ra khỏi lưới
                        if (!KhoTonKhoHelp.Check_DOFWObjRelDataInList(listObjRel, ObjRel))
                        {
                            flag = DAFWObjRelData.I.DeleteDetail(plTrans, ObjRel,ky_id);
                            if (flag == false)
                            {
                                plTrans.Rollback();
                                return flag;
                            }
                        }
                    }
                }                
            }
            return flag;
        }

        public bool Delete(DatabaseFB db, DbTransaction dbTrans, DOPhieuX DOX)
        {
            ITonKho data = (ITonKho)DOX;            
            long ky_id = KhoTonKhoHelp.GetKy(new PLTransaction(db, dbTrans), data.GetNgayPhatSinh());
            PLTransaction plTrans = new PLTransaction(db, dbTrans);
            //Danh sách tạo từ chương trình
            List<DOFWObjRelData> listObjRel = KhoTonKhoHelp.GetData(plTrans, 0, 0, DOX);

            //Cập nhật tồn kho từ kỳ hiện tại trở về sau
            foreach (DOFWObjRelData ObjRel in listObjRel)
                KhoTonKhoHelp.CapNhatNhapXuatChuyen(plTrans, ObjRel, 3,ky_id);           

            //Xóa phiếu phát sinh
            return DAFWObjRelData.I.Delete(new PLTransaction(db, dbTrans), 
                ky_id, DOX.GetID(), DOX.GetPhieuType().GetTypeID());             
        }
        
        public List<BizRule> GetUpdateRuleList()
        {
            return updateRuleList;
        }

        public List<BizRule> GetDeleteRuleList()
        {
            return deleteRuleList;
        }
        #endregion
    }
}
